package com.yuwei.module_property

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.yuwei.http.entity.TenantEntityItem
import com.yuwei.module_property.adapter.TenantAdapter
import com.yuwei.module_property.databinding.ActivityTenantBinding
import com.yuwei.module_property.viewmodel.PropertyViewModel
/**
 * 智慧园区-03-房源信息-租户页面-V1.1-20250508-邓孟卓
 */
@Route(path = RouterPath.MODULE_TENANT_ACTIVITY)
class TenantActivity : BaseMvvmActivity<ActivityTenantBinding>() {
    private val tenantAdapter = TenantAdapter()
    private val tenantList : MutableList<TenantEntityItem> = mutableListOf()
    private val vm : PropertyViewModel by viewModels()
    override fun initViews() {
        vm.getTenantList()
        binding.tenantBack.setOnClickListener {
            finish()
        }
        binding.tenantRv.adapter = tenantAdapter
        binding.tenantRv.layoutManager = LinearLayoutManager(this)
        binding.tenantSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                tenantAdapter.data.clear()
                val filter = tenantList.filter {
                    it.enterpriseName.contains(s.toString())
                }
                tenantAdapter.data.addAll(filter)
                tenantAdapter.notifyDataSetChanged()
            }

            override fun afterTextChanged(s: Editable?) {
                if(s?.length ==0){
                    vm.getTenantList()
                }
            }

        })
    }
    override fun observeDataChange() {
        vm.getTenantSuccess.observe(this){
            Log.d("TAG", "initDatattt: ${it.size}")
            tenantList.clear()
            tenantAdapter.data.clear()
            tenantList.addAll(it)
            tenantAdapter.data.addAll(it)
            tenantAdapter.notifyDataSetChanged()
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_tenant
}